package com.tbit.uqbike.service.business.impl;

import com.github.pagehelper.PageHelper;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.AccountUser;
import com.tbit.uqbike.object.pojo.MoneyOpeLog;
import com.tbit.uqbike.object.pojo.User;
import com.tbit.uqbike.object.pojo.statistics.ExportMoneyOpeLog;
import com.tbit.uqbike.service.business.MoneyOpeLogService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.AccountUserDao;
import com.tbit.uqbike.webmanager.dao.core.UserDao;
import com.tbit.uqbike.webmanager.dao.capital.MoneyOpeLogDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 手工资金操作记录(MoneyOpeLog)表服务实现类
 *
 * @author jing
 * @since 2020-11-20 13:44:18
 */
@Service("moneyOpeLogService")
public class MoneyOpeLogServiceImpl implements MoneyOpeLogService {
    @Resource
    private MoneyOpeLogDao moneyOpeLogDao;
    @Resource
    private AccountDao accountDao;
    @Resource
    private AccountUserDao accountUserDao;
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param accountId 主键
     * @return 实例对象
     */
    @Override
    public MoneyOpeLog queryById(Integer accountId) {
        return this.moneyOpeLogDao.queryById(accountId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<MoneyOpeLog> queryAllByLimit(int offset, int limit) {
        return this.moneyOpeLogDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param moneyOpeLog 实例对象
     * @return 实例对象
     */
    @Override
    public MoneyOpeLog insert(MoneyOpeLog moneyOpeLog) {
        this.moneyOpeLogDao.insert(moneyOpeLog);
        return moneyOpeLog;
    }

    /**
     * 修改数据
     *
     * @param moneyOpeLog 实例对象
     * @return 实例对象
     */
    @Override
    public MoneyOpeLog update(MoneyOpeLog moneyOpeLog) {
        this.moneyOpeLogDao.update(moneyOpeLog);
        return this.queryById(moneyOpeLog.getAccountId());
    }

    /**
     * 通过主键删除数据
     *
     * @param accountId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer accountId) {
        return this.moneyOpeLogDao.deleteById(accountId) > 0;
    }

    @Override
    public List<MoneyOpeLog> queryByMoneyOpeLog(MoneyOpeLog moneyOpeLog) {
        List<Integer> areas = accountDao.getById(moneyOpeLog.getAccountId()).stream().map(Account::getAccountId).collect(Collectors.toList());
        areas.add(moneyOpeLog.getAccountId());
        moneyOpeLog.setAreas(areas);
        PageHelper.startPage(moneyOpeLog.getPage().getPageNO(), moneyOpeLog.getPage().getRowCount());
        return moneyOpeLogDao.selectByByMoneyOpeLog(moneyOpeLog);
    }

    @Override
    public List<ExportMoneyOpeLog> export(MoneyOpeLog moneyOpeLog) {
        List<Integer> areas = accountDao.getById(moneyOpeLog.getAccountId()).stream().map(Account::getAccountId).collect(Collectors.toList());
        areas.add(moneyOpeLog.getAccountId());
        moneyOpeLog.setAreas(areas);
        List<ExportMoneyOpeLog> exports = moneyOpeLogDao.export(moneyOpeLog);
        if (null == exports) {
            return exports;
        }
        exports.stream().forEach(export -> {
            if (null != export) {
                if (null != export.getAccountUserId()) {
                    int accountUserId = Integer.parseInt(export.getAccountUserId());
                    AccountUser accountUser = accountUserDao.getAccountUserById(accountUserId);
                    if (null != accountUser) {
                        export.setAccountUserId(accountUser.getName());
                    } else {
                        export.setAccountUserId(null);
                    }
                }
                if (null != export.getUserId()) {
                    int userId = Integer.parseInt(export.getUserId());
                    User user = userDao.getByUserId(userId);
                    if (null != user) {
                        export.setUserId(user.getName());
                    } else {
                        export.setUserId(null);
                    }

                }
                switch (export.getMoneyType()) {
                    case "1":
                        export.setMoneyType("个人余额");
                        break;
                    case "2":
                        export.setMoneyType("礼品卡余额");
                        break;
                    case "3":
                        export.setMoneyType("押金");
                        break;
                    case "4":
                        export.setMoneyType("欠款");
                        break;
                    default:
                        export.setMoneyType("");
                }
                export.setChangeType("1".equals(export.getChangeType()) ? "充值" : "扣款");
            }
        });
        return exports;
    }
}